package com.web.controller;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.common.entity.Developer;
import com.common.entity.User;
import com.web.mapper.UserMapper;
import io.github.coho04.githubapi.Github;

import io.github.coho04.githubapi.entities.GHUser;

import lombok.extern.slf4j.Slf4j;
import org.junit.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Description
 * @Author nomo
 * @Date 2024/10/24 9:16
 */
@RestController
@Slf4j
public class TestController {

    @Autowired
    private UserMapper userMapper;

    /*
    * 测试SpringBoot
    * */
    @RequestMapping("/test")
    private String test() {
        System.out.println("sout:777");
        log.info("999");
        return "666";
    }

    /*
     *  测试GithubAPI接口
     * */
    @RequestMapping("/GithubUser")
    private String githubTest(){
        //TODO: 输入GitHub 用户名
        String username = "nomop";

        final String GITHUB_API_URL = "https://api.github.com/users/";
        final String TOKEN = "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";

        // 构建完整的 URL
        String url = GITHUB_API_URL + username;

        // 使用 Hutool 发起 GET 请求
        HttpResponse response = HttpRequest.get(url)
                .header("Authorization", "token " + TOKEN)  // 设置 GitHub Token 以避免限流
                .header("Accept", "application/vnd.github.v3+json")  // 设置接受的数据格式
                .timeout(20000)  // 设置超时时间（毫秒）
                .execute();


        //TODO:包装成VO类

        // 检查响应状态码
        if (response.getStatus() == 200) {
            return "用户信息: " + response.body();
        } else {
            return "请求失败，状态码: " + response.getStatus();
        }
    }

    @RequestMapping("/GithubAPI")
    private String GithubAPI(){
        Github github = new Github("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");
        GHUser user = github.findUserByName("nomop");
        log.info(user.getReposUrl());
        return null;
    }

    @RequestMapping("/data")
    private String getStatistic(){
        String username = "nomop"; // 替换为你的 GitHub 用户名
        String apiUrl = "https://github-readme-stats.vercel.app/api?username=" + username;

        // 发送 GET 请求
        HttpResponse response = HttpRequest.get(apiUrl)
                .header("Accept", "application/vnd.github.v3+json")  // 设置接受的数据格式
                .timeout(20000)  // 设置超时时间（毫秒）
                .execute();

        // 检查响应状态
        if (response.getStatus() == 200) {
            // 获取返回的内容
            String jsonResponse = response.body();
            System.out.println("GitHub Stats: " + jsonResponse);
        } else {
            System.out.println("请求失败，响应代码：" + response.getStatus());
        }

        return response.body();
    }


}
